<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 支付控制器
 * @package app\index\controller
 */
class PayConfig extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取付款列表
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data = Db::name('qt_pay_config')
                ->alias('c')
                ->leftJoin('qt_users u','u.id = c.user_id')
                ->leftJoin('qt_pay_type t','c.type = t.id')
                ->field('c.id,c.name,c.type,c.sort,c.status,c.pay,u.real_name,t.name as pay_name,c.create_time')
                ->where(['c.store_id' => $userInfo['store_id']])
                ->order('c.id desc')
                ->select();
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }




    /**
     *获取付款列表
     * @return array
     */
    public function type()
{
    if ($this->request->isPost()) {
        $data = Db::name('qt_pay_type')
            ->field('t.id,t.name,t.index_show,t.store_id')
            ->alias('t')
            ->leftJoin('qt_pay_config p','p.pay_type_id = t.id')
            ->where(['t.store_id'=>$this->userInfo['store_id'],'t.status'=>'1','t.type'=>'2'])
            ->order('p.sort asc')
            ->select();
        $orl = Db::name('qt_pay_type')->field('id,name,index_show,store_id')->where(['store_id'=>'0','status'=>'1'])->order('id asc')->select();
        $result = array_merge($orl,$data);
        $this->result('查询成功', $result, 200);
    }
    $this->result('无效的请求', '', 204);
}

    /**
     * 添加付款方式
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['name']) || empty($getData['type'])){
                $this->result('缺少参数', '', 204);
            }
            $getData['user_id'] = $this->userInfo['id'];
            $getData['store_id'] = $this->userInfo['store_id'];
            $getData['create_time'] = time();
            $insert = Db::name('qt_pay_config')->insert($getData);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑保存付款方式
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['name']) || empty($getData['type']) || empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $update= Db::name('qt_pay_config')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑保存付款方式
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_pay_config')->where('id',$getData['id'])->find();
            $this->result('获取成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除付款方式
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_pay_config')->where('id',$getData['id'])->delete();
            $this->result('删除成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }



}
